package com.jingtai.shop.service.goods.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.*;
import com.jingtai.common.utils.MyBeanUtils;
import com.jingtai.common.webfacade.error.CommonException;
import com.jingtai.core.entity.member.MemberCollectStore;
import com.jingtai.core.entity.merchants.UserStore;
import com.jingtai.core.entity.shop.MerchantsGoods;
import com.jingtai.core.mapper.merchants.UserStoreMapper;
import com.jingtai.shop.common.template.ShopBaseController;
import com.jingtai.shop.service.goods.MerchantsGoodsService;
import com.jingtai.shop.service.goods.UserStoreService;
import com.jingtai.shop.service.user.MemberCollectStoreService;
import com.jingtai.shop.webfacade.vo.collect.store.ListStoreGoodsVO;
import com.jingtai.shop.webfacade.vo.collect.store.StoreDetailsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserStoreServiceImpl extends ServiceImpl<UserStoreMapper, UserStore> implements UserStoreService {

    @Resource
    private UserStoreService storeService;
    @Resource
    private MemberCollectStoreService memberCollectStoreService;
    @Resource
    private MerchantsGoodsService merchantsGoodsService;

    /**
     * 查看店铺详情
     */
    @Override
    public StoreDetailsVO showStoreDetails(Long storeId,String goodsTitle,Long userId) {
        UserStore userStore = storeService.getById(storeId);
        if (ObjectUtil.isEmpty(userStore)) {
            throw new CommonException("店铺未查到!");
        }
        StoreDetailsVO storeDetailsVO = MyBeanUtils.copyPropertiesChaining(userStore, StoreDetailsVO::new);
        //查询总收藏数量
        int count = memberCollectStoreService.count(Wrappers.lambdaQuery(MemberCollectStore.class).eq(MemberCollectStore::getStoreId, storeId).eq(MemberCollectStore::getIsCollect, CollectStatusEnum.COLLECTED.getStatus()).eq(MemberCollectStore::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        storeDetailsVO.setCollectionSize(count);
        //查询本人收藏状态
        int count1 = memberCollectStoreService.count(Wrappers.lambdaQuery(MemberCollectStore.class).eq(MemberCollectStore::getUserId,userId).eq(MemberCollectStore::getStoreId, storeId).eq(MemberCollectStore::getIsCollect, CollectStatusEnum.COLLECTED.getStatus()).eq(MemberCollectStore::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        storeDetailsVO.setCollectionState(count1 > 0 ? 1 : 0);
        //查询新品上架数量
        int count2 = merchantsGoodsService.count(Wrappers.lambdaQuery(MerchantsGoods.class).eq(MerchantsGoods::getGoodsState, GoodsStatusEnum.NORMAL.getStatus()).eq(MerchantsGoods::getIsNewProducts, NewProductsEnum.TRUE.getStatus()).eq(MerchantsGoods::getDeleteFlag, DeleteFlagEnum.NOT_DELETE));
        storeDetailsVO.setNewProductsSize(count2);
        //查询活动促销数量
        int count3 = merchantsGoodsService.count(Wrappers.lambdaQuery(MerchantsGoods.class).eq(MerchantsGoods::getGoodsState, GoodsStatusEnum.NORMAL.getStatus()).eq(MerchantsGoods::getIsActivity, ActivityEnum.TRUE.getStatus()).eq(MerchantsGoods::getDeleteFlag, DeleteFlagEnum.NOT_DELETE));
        storeDetailsVO.setActivitySize(count3);
        //查询总商品数量
        int count4 = merchantsGoodsService.count(Wrappers.lambdaQuery(MerchantsGoods.class).eq(MerchantsGoods::getStoreId, storeId).eq(MerchantsGoods::getGoodsState, GoodsStatusEnum.NORMAL.getStatus()).eq(MerchantsGoods::getDeleteFlag, DeleteFlagEnum.NOT_DELETE));
        storeDetailsVO.setTotalGoodsSize(count4);
        return storeDetailsVO;
    }


}
